package com.lvy.hczn.transmit.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lvy.hczn.front.common.constant.MqConstants;
import com.lvy.hczn.front.common.exception.UtilException;
import com.lvy.hczn.mq.rocket.ParamInfo;
import com.lvy.hczn.mq.rocket.ProducerUtil;
import com.lvy.hczn.transmit.domain.ProductSupplierGoodsBarcode;
import com.lvy.hczn.transmit.mapper.ProductSupplierGoodsBarcodeMapper;
import com.lvy.hczn.transmit.service.ProductSupplierGoodsBarcodeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;
import java.util.List;

@Service
@Slf4j
public class ProductSupplierGoodsBarcodeServiceImpl extends ServiceImpl<ProductSupplierGoodsBarcodeMapper, ProductSupplierGoodsBarcode> implements ProductSupplierGoodsBarcodeService {

    @Autowired
    private ProductSupplierGoodsBarcodeMapper productSupplierGoodsBarcodeMapper;

    @Autowired
    private ProducerUtil producerUtil;

    /**
     * 商品规格同步
     *
     * @param request
     * @param info
     * @return void
     * @author yw
     * @date 2023-06-05 16:34:31
     */
    @Override
    public void syncSpec(HttpServletRequest request, ParamInfo info, boolean sync) {
        if (info.getData() == null && (info.getList() == null || info.getList().isEmpty())) {
            throw new UtilException("数据为空");
        }
        log.error("syncSpec:{}", JSONUtil.toJsonStr(info));
        if (info.getList() != null && !info.getList().isEmpty()) {
            List<ProductSupplierGoodsBarcode> list = JSON.parseArray(JSON.toJSONString(info.getList()), ProductSupplierGoodsBarcode.class);
            for (ProductSupplierGoodsBarcode item : list) {
                saveOrUpdateBarcode(item);
            }
        } else if (info.getData() != null) {
            ProductSupplierGoodsBarcode item = JSON.parseObject(JSON.toJSONString(info.getData()), ProductSupplierGoodsBarcode.class);
            saveOrUpdateBarcode(item);
        }
        if (sync) {
            try {
                producerUtil.sendAsyncMsg(MqConstants.TOPIC_SYNC_SKU, null, JSON.toJSONString(info).getBytes(StandardCharsets.UTF_8), IdUtil.fastSimpleUUID());
            } catch (Exception e) {
                throw new UtilException("信息转发异常");
            }
        }
    }

    private void saveOrUpdateBarcode(ProductSupplierGoodsBarcode item) {
        if (StrUtil.isEmpty(item.getSiteCode())) {
            throw new UtilException("站点编码为空");
        }
        if (StrUtil.isEmpty(item.getSupplierCode())) {
            throw new UtilException("供应商编码为空");
        }
        if (StrUtil.isEmpty(item.getProductSysCode())) {
            throw new UtilException("商品编码为空");
        }
        if (StrUtil.isEmpty(item.getBarcodeSysCode())) {
            throw new UtilException("sku编码为空");
        }
        log.error("saveOrUpdateBarcode:{}", JSONUtil.toJsonStr(item));
        ProductSupplierGoodsBarcode supplierGoodsBarcode = super.getOne(Wrappers.<ProductSupplierGoodsBarcode>lambdaQuery().eq(ProductSupplierGoodsBarcode::getBarcodeSysCode, item.getBarcodeSysCode()).eq(ProductSupplierGoodsBarcode::getSiteCode, item.getSiteCode()));
        if (supplierGoodsBarcode != null) {
            item.setId(supplierGoodsBarcode.getId());
            super.updateById(item);
        } else {
            if(!StrUtil.isEmpty(item.getBarcodeId())){
                item.setId(item.getBarcodeId());
            }
            super.save(item);
        }
    }
}
